# -- coding: utf-8 --
import platform
import sys
from datetime import datetime
import random
from ctypes import *

path = "./resources/lib/"
if 'Windows' in platform.system():
    if "32bit" in platform.architecture():
        Psyuunew=windll.LoadLibrary(path+'Syunew3D.dll')
    else:
        Psyuunew=windll.LoadLibrary(path+'Syunew3D_x64.dll')
else:
    if "32bit" in platform.architecture():
        Psyuunew=windll.LoadLibrary(path+'libPsyunew3.so')
    else:
       Psyuunew=cdll.LoadLibrary(path+'libPsyunew3_64.so')

##获到锁的版本
NT_GetIDVersion=Psyuunew.NT_GetIDVersion
NT_GetIDVersion.argtypes=(c_void_p,c_char_p)
NT_GetIDVersion.restypes=(c_int)


##获取锁的扩展版本
NT_GetVersionEx=Psyuunew.NT_GetVersionEx
NT_GetVersionEx.argtypes=(c_void_p,c_char_p)
NT_GetVersionEx.restypes=(c_int)


##算法函数
sWrite_2Ex=Psyuunew.sWrite_2Ex  
sWrite_2Ex.argtypes=(c_ulong ,c_void_p,c_char_p)
sWrite_2Ex.restypes=(c_int)

sWriteEx=Psyuunew.sWriteEx  
sWriteEx.argtypes=(c_ulong ,c_void_p,c_char_p)
sWriteEx.restypes=(c_int)

sRead=Psyuunew.sRead  
sRead.argtypes=(c_void_p,c_char_p)
sRead.restypes=(c_int)

sWrite_2=Psyuunew.sWrite  
sWrite_2.argtypes=(c_ulong ,c_char_p)
sWrite_2.restypes=(c_int)

sWrite_2=Psyuunew.sWrite_2  
Psyuunew.argtypes=(c_ulong ,c_char_p)
sWrite_2.restypes=(c_int)
##算法函数

##写一个字节到加密锁中
YWrite=Psyuunew.YWrite
YWrite.argtypes=(c_byte ,c_short,c_char_p ,c_char_p,c_char_p )
YWrite.restypes=(c_int)

##从加密锁中读取一个字??
YRead=Psyuunew.YRead
YRead.argtypes=(c_void_p,c_short,c_char_p ,c_char_p,c_char_p )
YRead.restypes=(c_int)

##写一个字节到加密锁中
YWriteEx=Psyuunew.YWriteEx
YWriteEx.argtypes=(c_void_p,c_short,c_short,c_char_p,c_char_p,c_char_p )
YWriteEx.restypes=(c_int)

##从加密锁中读取一批字??
YReadEx=Psyuunew.YReadEx
YReadEx.argtypes=(c_void_p,c_short,c_short,c_char_p,c_char_p,c_char_p )
YReadEx.restypes=(c_int)

##查找指定的加密锁（使得普通算法一??
FindPort_2=Psyuunew.FindPort_2  
FindPort_2.argtypes=(c_int ,c_ulong ,c_ulong ,c_char_p)
FindPort_2.restypes=(c_int)

##查找加密??
FindPort=Psyuunew.FindPort  
FindPort.argtypes=(c_int ,c_char_p)
FindPort.restypes=(c_int)

##获取锁的ID
GetID=Psyuunew.GetID  
GetID.argtypes=(c_void_p,c_void_p,c_char_p)
GetID.restypes=(c_int)

##从加密锁中读字符??
YReadString=Psyuunew.YReadString 
YReadString.argtypes=(c_char_p ,c_short,c_int ,c_char_p ,c_char_p,c_char_p)
YReadString.restypes=(c_int)

##写字符串到加密锁??
YWriteString=Psyuunew.YWriteString
YWriteString.argtypes=(c_char_p,c_short,c_char_p ,c_char_p,c_char_p )
YWriteString.restypes=(c_int)

##设置写密??
SetWritePassword=Psyuunew.SetWritePassword
SetWritePassword.argtypes=(c_char_p ,c_char_p,c_char_p ,c_char_p,c_char_p)
SetWritePassword.restypes=(c_int)

##设置读密??
SetReadPassword=Psyuunew.SetReadPassword
SetReadPassword.argtypes=(c_char_p ,c_char_p,c_char_p ,c_char_p,c_char_p)
SetReadPassword.restypes=(c_int)

##设置增强算法密钥一
SetCal_2=Psyuunew.SetCal_2
SetCal_2.argtypes=(c_char_p,c_char_p)
SetCal_2.restypes=(c_int)

##使用增强算法一对字符串进行加密
EncString=Psyuunew.EncString  
EncString.argtypes=(c_char_p,c_char_p,c_char_p)
EncString.restypes=(c_int)

##使用增强算法一对二进制数据进行加密
Cal=Psyuunew.Cal  
Cal.argtypes=(c_void_p,c_void_p,c_char_p)
Cal.restypes=(c_int)

##设置增强算法密钥??
SetCal_New=Psyuunew.SetCal_New
SetCal_New.argtypes=(c_char_p,c_char_p)
SetCal_New.restypes=(c_int)

##使用增强算法二对字符串进行加??
Cal_New=Psyuunew.Cal_New  
Cal_New.argtypes=(c_void_p,c_void_p,c_char_p)
Cal_New.restypes=(c_int)

##使用增强算法二对字符串进行加??
EncString_New=Psyuunew.EncString_New  
EncString_New.argtypes=(c_char_p,c_char_p,c_char_p)
EncString_New.restypes=(c_int)

##返回锁的出厂编码
GetProduceDate=Psyuunew.GetProduceDate  
GetProduceDate.argtypes=(c_char_p,c_char_p)
GetProduceDate.restypes=(c_int)

##设置ID种子
SetID=Psyuunew.SetID
SetID.argtypes=(c_char_p ,c_char_p)
SetID.restypes=(c_int)

##设置普通算??
SetCal=Psyuunew.SetCal
SetCal.argtypes=(c_char_p ,c_char_p,c_char_p ,c_char_p,c_char_p)
SetCal.restypes=(c_int)

##生产日期转换函数
##SnToProduceDate=Psyuunew.SnToProduceDate
##SnToProduceDate.argtypes=(c_char_p ,c_char_p )
##SnToProduceDate.restypes=(c_void)

##使用增强算法对字符串进行解密使用软件
##StrDec=Psyuunew.StrDec
##StrDec.argtypes=(c_char_p,c_char_p,c_char_p)
##StrDec.restypes=(c_void )
##
##StrEnc=Psyuunew.StrEnc  
##StrEnc.argtypes=(c_char_p,c_char_p,c_char_p)
##StrEnc.restypes=(c_void)
##
##EnCode=Psyuunew.EnCode    
##EnCode.argtypes=(c_void_p ,c_void_p ,  c_char_p )
##EnCode.restypes=(c_void)
##
##DeCode=Psyuunew.DeCode   
##DeCode.argtypes=(c_void_p , c_void_p , c_char_p  )
##DeCode.restypes=(c_void)
##使用增强算法对字符串进行解密使用软件)


##使用增强算法对二进制数据进行加密使用软件)
##DecBySoft=Psyuunew.DecBySoft         
##DecBySoft.argtypes=(c_void_p, c_void_p )

##EncBySoft=Psyuunew.EncBySoft         
##EncBySoft.argtypes=(c_void_p   ,  c_void_p   )
##使用增强算法对二进制数据进行加密使用软件)

##字符串及二进制数据的转换
##HexStringToc_byteArray=Psyuunew.HexStringToc_byteArray
##HexStringToc_byteArray.argtypes=(c_char_p ,c_void_p)
##HexStringToc_byteArray.restypes=(c_void)
##
##ByteArrayToHexString=Psyuunew.ByteArrayToHexString
##ByteArrayToHexString.argtypes=(c_void_p,c_char_p ,c_int )
##ByteArrayToHexString.restypes=(c_void)
##字符串及二进制数据的转换

 ##初始化锁函数,注意，初始化锁后，所有的数据??，读写密码也??0000000-00000000，增强算法不会被初始??
ReSet=Psyuunew.ReSet
ReSet.argtypes=[c_char_p]
ReSet.restypes=(c_int)

##以下函数只限于带U盘的??
##设置是否显示U盘部分盘符，真为显示，否为不显示
SetHidOnly=Psyuunew.SetHidOnly 
SetHidOnly.argtypes=( c_bool,c_char_p)
SetHidOnly.restypes=(c_int)

##设置U盘部分为只读状态，
SetUReadOnly=Psyuunew.SetUReadOnly 
SetUReadOnly.argtypes=[c_char_p]
SetUReadOnly.restypes=(c_int)
##以上函数只限于带U盘的??

##以下函数只支持智能芯片的??
##生成SM2密钥??
YT_GenKeyPair=Psyuunew.YT_GenKeyPair
YT_GenKeyPair.argtypes=(c_char_p ,c_char_p,c_char_p,c_char_p)
YT_GenKeyPair.restypes=(c_int)

##设置Pin??
YtSetPin=Psyuunew.YtSetPin
YtSetPin.argtypes=(c_char_p,c_char_p,c_char_p )
YtSetPin.restypes=(c_int)

##设置SM2密钥对及身份
Set_SM2_KeyPair=Psyuunew.Set_SM2_KeyPair
Set_SM2_KeyPair.argtypes=(c_char_p,c_char_p,c_char_p,c_char_p,c_char_p )
Set_SM2_KeyPair.restypes=(c_int)

##返回加密锁的公钥
Get_SM2_PubKey=Psyuunew.Get_SM2_PubKey
Get_SM2_PubKey.argtypes=(c_char_p,c_char_p,c_char_p,c_char_p)
Get_SM2_PubKey.restypes=(c_int)

##对二进制数据进行SM2加密
SM2_EncBuf=Psyuunew.SM2_EncBuf
SM2_EncBuf.argtypes=(c_void_p,c_void_p,c_int ,c_char_p)
SM2_EncBuf.restypes=(c_int)

##对二进制数据进行SM2解密
SM2_DecBuf=Psyuunew.SM2_DecBuf
SM2_DecBuf.argtypes=(c_void_p,c_void_p,c_int ,c_char_p ,c_char_p)
SM2_DecBuf.restypes=(c_int)

##对字符串进行SM2加密
SM2_EncString=Psyuunew. SM2_EncString
SM2_EncString.argtypes=(c_char_p,c_char_p,c_char_p)
SM2_EncString.restypes=(c_int)

##对字符串进行SM2解密
SM2_DecString=Psyuunew.SM2_DecString
SM2_DecString.argtypes=(c_char_p,c_char_p,c_char_p ,c_char_p)
SM2_DecString.restypes=(c_int)

##对消息进行SM2签名
YtSign=Psyuunew.YtSign
YtSign.argtypes=(c_char_p , c_char_p  ,c_char_p ,c_char_p)
YtSign.restypes=(c_int)

##对SM2签名进行验签
YtVerfiy=Psyuunew.YtVerfiy
YtVerfiy.argtypes=(c_char_p,c_char_p,c_char_p,c_char_p,c_char_p,c_void_p,c_char_p)
YtVerfiy.restypes=(c_int)

##SM2算法初始化(使用软件)
IniSM2=Psyuunew.IniSM2
IniSM2.restypes=(c_int)

##对字符串进行SM2解密(使用软件)
SM2EncString=Psyuunew.SM2EncString
SM2EncString.argtypes=(c_char_p,c_char_p,c_char_p ,c_char_p)
SM2EncString.restypes=(c_int)


##对字符串进行SM2加密（使用软件）
SM2DecString=Psyuunew.SM2DecString
SM2DecString.argtypes=(c_char_p,c_char_p,c_char_p)
SM2DecString.restypes=(c_int)

##对消息进行SM2签名（使用软件）
SM2Sign=Psyuunew.SM2Sign
SM2Sign.argtypes=(c_char_p , c_char_p ,c_int ,c_char_p ,c_char_p)
SM2Sign.restypes=(c_int)

##对SM2签名进行验签（使用软件）
SM2Verfiy=Psyuunew.SM2Verfiy
SM2Verfiy.argtypes=(c_char_p,c_char_p,c_char_p,c_char_p,c_char_p)
SM2Verfiy.restypes=(c_int)

##释放SM2算法(使用软件)
ReleaseSM2=Psyuunew.ReleaseSM2

##返回锁的硬件芯片唯一ID
GetChipID=Psyuunew.GetChipID 
GetChipID.argtypes=(c_char_p,c_char_p)
GetChipID.restypes=(c_int)
##以上函数只支持智能芯片的锁

##以下函数只支持D8
CheckDate=Psyuunew.CheckDate
CheckDate.argtypes=(c_char_p,c_char_p)
CheckDate.restypes=(c_int)
##以上函数只支持D8


if 'Linux' in platform.system():
	CloseUsbHandle=Psyuunew.CloseUsbHandle
	CloseUsbHandle.argtype=c_char_p
	CloseUsbHandle.restypes=(c_void_p)

def CheckDateEx(Path):
    dt=datetime.now()
    nowDate=dt.strftime( '%Y/%m/%d/%H/%M/%S' )
    ret=CheckDate(nowDate.encode('utf-8'),Path)
    return ret

def HexStringToByteArrayEx(InString):
    
    mylen=len(InString)
    array_data={}
    in_data=c_byte
    temp=''
    for n in range(0,mylen,2):
        temp=InString[n:2+n]
        temp='0x'.encode()+temp
        in_data=int(temp,16)
        array_data[n/2]=(in_data)
    return array_data

def StringToByteArray(InString):
    
    mylen=len(InString)
    array_data={}
    in_data=c_int
    temp=''
    for n in range(0,mylen):
        temp=InString[n:1+n]
        in_data=ord(temp)
        array_data[n]=(in_data)
    array_data[n+1]=0
    return array_data

def ByteArrayToString(InBuf):
    arrBytes = bytearray()
    for n in range(0,len(InBuf)):
        arrBytes.append(InBuf[n])
    return arrBytes.decode()
    

def ByteArrayToHexString(in_data,inlen):
    OutString=''
    temp=''
    for n in range(0,inlen):
        temp='%02X' % in_data[n]
        OutString=OutString+temp
    return OutString

def EnCode(InData,Key,pos):
    KeyBuf=HexStringToByteArrayEx(Key)
    OutData=EncBySoft(InData,KeyBuf,pos)	
    return OutData

def DeCode(InData,Key,pos):
    KeyBuf=HexStringToByteArrayEx(Key)
    OutData=DecBySoft(InData,KeyBuf,pos)
    return OutData


def EncBySoft(inb, KeyBuf,pos):
    bufArray=c_uint32*16
    buf=bufArray

    temp_string=''
    cnDelta = 2654435769
    _sum = 0
    a = 0
    b = 0
    c = 0
    d = 0
 
    for n in range(0,4):
        a = (KeyBuf[n] << (n * 8)) | a
        b = (KeyBuf[n + 4] << (n * 8)) | b
        c = (KeyBuf[n + 4 + 4] << (n * 8)) | c
        d = (KeyBuf[n + 4 + 4 + 4] << (n * 8)) | d 
    y = 0
    z = 0

    for n in range(0,4):
        temp = (inb[pos +n])
        y = (temp << (n * 8)) | y
        temp = (inb[pos +n + 4])
        z = (temp << (n * 8)) | z

    n = 32
    while  n > 0:
        _sum = (cnDelta + _sum) & 0xffffffff
        temp=(z << 4) & 0xffffffff
        temp=(temp+a)& 0xffffffff
        temp_1= (z + _sum) & 0xffffffff
        temp_2=((z >> 5) + b)& 0xffffffff
        temp=temp ^ temp_1 ^ temp_2
        y = (y+ temp)& 0xffffffff
        temp=(y << 4)& 0xffffffff
        temp=(temp + c)& 0xffffffff
        temp_1=(y + _sum)& 0xffffffff
        temp_2=((y >> 5) + d)& 0xffffffff
        temp=temp ^ temp_1 ^ temp_2
        z=(z+temp)& 0xffffffff
        n = n - 1

    outb={}
    for n in range(0,4):
        outb [n]= (y >> (n) * 8) & 255
        outb[n + 4] = (z >> (n) * 8) & 255

    return outb

def DecBySoft(inb, KeyBuf,pos):
    bufArray=c_uint32*16
    buf=bufArray
    temp_string=''
    cnDelta = 2654435769
    _sum = 0xC6EF3720
    a = 0
    b = 0
    c = 0
    d = 0
    for n in range(0,4):
        a = (KeyBuf[n] << (n * 8)) | a
        b = (KeyBuf[n + 4] << (n * 8)) | b
        c = (KeyBuf[n + 4 + 4] << (n * 8)) | c
        d = (KeyBuf[n + 4 + 4 + 4] << (n * 8)) | d
    y = 0
    z = 0
    for n in range(0,4):
        temp = (inb[pos +n])
        y = (temp << (n * 8)) | y
        temp = inb[pos +n + 4]
        z = (temp << (n * 8)) | z

    n = 32
    while  n > 0:
        temp=(y << 4)
        temp= ( temp+ c) & 0xffffffff
        temp_1=(y + _sum)& 0xffffffff
        temp_2=((y >> 5) + d)& 0xffffffff
        temp= temp ^ temp_1 ^ temp_2
        z=(z-temp) & 0xffffffff
        #z -= ((y << 4) + c) ^ (y + _sum) ^ ((y >> 5) + d)
        temp=(z << 4)& 0xffffffff
        temp=(temp+a)& 0xffffffff
        temp_1=(z + _sum)& 0xffffffff
        temp_2=((z >> 5) + b)& 0xffffffff
        temp= temp ^ temp_1 ^ temp_2
        y = (y -temp)& 0xffffffff
        _sum = (_sum -cnDelta)& 0xffffffff
        n = n - 1
    
    outb={}
    for n in range(0,4):
        outb[n] = (y >> (n) * 8) & 255
        outb[n + 4] = (z >> (n) * 8) & 255

    return outb

def StrDec(InString,Key):
   OutBuf={}
   mylen=len(InString)/2
   KeyBuf=HexStringToByteArrayEx(Key)
   InBuf=HexStringToByteArrayEx(InString)
   for n in range(0,(mylen-8)+1,8):
        tempBuf=DecBySoft(InBuf,KeyBuf,n)
        for i in range(0,8):
             OutBuf[i+ n] = tempBuf[i]
   if mylen>8:
       for n in range(len(OutBuf),mylen):
            OutBuf[n]=(InBuf[n])
   return ByteArrayToString(OutBuf)


def StrEnc(InString,Key):
    OutBuf={}
    InBuf={}
    temp_Buf=InString.encode('utf-8')
    mylen=len(InString)+1
    for n in range(0,mylen-1):
        InBuf[n]=temp_Buf[n]
    InBuf[n+1]=(0)
    if mylen<8:
            for n in range(mylen,8):
                 InBuf[n]=(0)
            mylen=8
            
    KeyBuf=HexStringToByteArrayEx(Key)
    for n in range(0,(mylen-8)+1,8):
         tempBuf=EncBySoft(InBuf,KeyBuf,n)
 
         for i in range(0,8):
              OutBuf[i+ n] = tempBuf[i]
    if mylen>8:
       for n in range(len(OutBuf),mylen-1):
            OutBuf[n]=(InBuf[n])
       OutBuf[n+1]=0
    OutString=ByteArrayToHexString(OutBuf,mylen)
    return OutString

#使用增强算法二检查是否存在对应的加密锁
def CheckKeyByEncstring_New():
    DevicePath=create_string_buffer(260)
    EncInString=["3064","4372","24172","6234","23727","12626","26232","26076","25390","28862","20348","21754","14173","12186","177","6149","2630","712","17264","15086","19","9673","27956","9543","10766","3264","10649","15427","8353","5742",  \
"25685","9397","15703","24118","21318","9484","29912","11802","8291","10076","20719","4283","13256","23382","2696","860","7597","30798","17321","18398","8302","7988","24959","22212","23171","23706","1620","28975","4233","19124",  \
"11152","21219","5943","21330","23486","1088","23911","4583","3209","19528","5654","21891","6329","17055","604","17115","23097","389","23410","5696","25620","30683","23731","1443","23667","23335","28058","17046","30368","14728",  \
"27584","7245","3334","11200","5344","16791","23341","10034","4306","8937","22464","28639","7057","29779","11914","10859","20279","20360","23156","3606","3583","30169","24456","14018","3138","12857","9456","5874","31353","23354",  \
"28406","2504","20288","29965","30980","1286","12624","29573","12197","27404","388","7188","2689","31966","1236","1995","1636","27678","11867","17756","16182","3343","2388","6939","10755","10384","17175","26871","28404","28197",  \
"21792","10886","25427","1520","20232","6390","9099","17074","16521","27930","19748","29646","17277","16742","22729","22341","9695","5099","5189","11195","13255","26713","15489","25199","8561","2565","14214","28998","5429","28126",  \
"28288","17600","20879","13433","18360","20992","32535","19462","16537","18631","5770","12888","10155","25976","27327","22240","30361","25364","21169","7346","19502","3391","29562","31869","1619","29240","10837","18070","31761","13540",  \
"5787","1261","22421","32451","17350","32020","4134","4600","29551","21055","1072","31556","1875","18238","13114","25610","2889","8323","6130","1100","24396","9762","8736","13483","375","17866","20653","8298","30722","21711",  \
"27557","5995","23368","29219","24113","47","32708","2137","12443","27096","16399","28575","8782","15913","26339","12235","25553","4997","14619","25165","28882","7915","8111","13369","2585","17919","29114","2111","20367","20739",  \
"24183","14513","31296","15576","30526","31106","13909","11522","22923","23107","7449","32160","30967","26664","20212","30070","2365","28672","11633","6119","2101","24640","13627","5874","26585","19192","30746","25915","22598","2012",  \
"14074","26628","12724","30266","7140","18734","32380","24328","5122","26940","9498","30423","18846","29008","12839","32728","15906","1324","16989","3039","1049","5425","3223","24155","6443","20577","3021","28256","24611","13919",  \
"18714","2208","15084","18797","17848","4460","17551","24684","1283","26295","11062","6368","13937","25107","10795","26856","32670","31409","9177","20808","26182","1892","23495","12131","23104","8350","20643","12088","24455","8816",  \
"13877","23915","22582","20108","3943","22846","28293","3654","26690","9875","7888","21646","12274","8171","24975","8183","12992","2153","30046","7346","13335","15822","18679","26257","10367","2685","1330","27400","4004","6007",  \
"14090","27431","28238","32351","6554","7585","26728","31607","27","28871","31485","16289","24549","27788","22070","25181","31350","13442","22366","29648","10087","10714","8925","11963","15983","11623","24126","19071","8556","411",  \
"2604","27934","26197","19591","31855","15675","8518","14813","31219","5145","20087","15601","28865","20374","23619","29362","20995","17465","21797","9142","3075","20256","10678","26154","9259","2064","6833","24548","28933","4969",  \
"17133","10325","29756","5166","16744","19086","21656","15871","826","26434","30725","1826","11676","30402","15756","31348","10218","11010","22359","10620","17820","29073","17584","15960","4174","5540","27649","11725","12729","523",  \
"21717","2363","32183","9016","469","28836","7235","26941","27338","9388","21365","27086","5792","14482","29064","8764","21855","25658","22231","22904"]
    EncOutString=["360DD5DA60D43DD4","EF583F254F170637","7868C8A2A767170F","3F2E2EAD4188EBB4","D734BAFA3AEEDFF6","862C9B78BD903527","E985432B67A9A351","23E021E3A170DFED","2BCDC84253D96A5E","3247DC314058FF98","B3F8662858E60A55","12F3AF3FF7193320","CFAB0BEE4E5CFE9D","D7671A8B7C6C34F4","F9CC67ED33B50311","EDE5C76E31272290","8A7559486E18A556","2E5144A488DB2F78","5CF9508D957E987C","49377E77B3CDB4F8","A602DA0C0B9B1458","19948717A47BE7B3","E680DA4598592F8B","378040C984D04BD0","658D808AA8681C83","AB133BA68C2A86A2","9DDECAC506C11337","C041ADF542CC0C04","4C3BB58FE851382E","BDB3E1338984D967",  \
"45710BE426B8D235","100BCE985B1AA56B","A70A0E144E57C274","62C3AEA387C4DFF4","243C0B81DFAE8928","52CBEB5A3AAFEDA2","51183811C24665A8","BC82C74ABCF9C4D9","615B57F1D4F7FC95","DC10AACA9FCF91CF","FCA89D0549772C4E","975BA029565E464E","9F9F65DAD6C05ECE","5CFAD307A4D527DB","684069B63415322B","4FF6AF9124442429","94F27468DB785171","2D836A1DAFD4170D","6051EE947371FED4","D9545DF5752D26D3","8DB907ADA4514603","FF3863096AAB05F0","9E123E08DB7FBE8F","E5C43B05C5BE908C","D4FCB6DCCC3CAED3","6FF73E37AB00CF23","494D8BA5FF0C8857","6F505B360C4E1799","687A93E825EEAF76","8180F801B3505405",  \
"B491853B148EB264","657B4753A2DBC7B9","9C3B4B455E26DC0A","5D2EE27ED279D1AC","C64E24B750E4DC6B","35F1CA7A5B68F897","FE7494BBDEE96AB1","F71B471A33AB8166","E01362C6C05522D9","949CD3A272D10903","6A3E2290A2260977","9815F5C6D18A71C0","072F805DA82478F9","8C3D3F2AB2F90CD9","9DBE5C088CF8B9E9","AE63EEDEE7730C6E","245AE3133E99669B","4EE4977CBA733250","E1E154F9998F7652","26B9134A63D1B30D","09BDB10CE7C727E8","026DDC3631829B02","6037A3D12D465796","53D87C6909495A11","7FD6AA6E635673F3","882E250A61F5393A","5CF6BF7E305C7F84","649CB094326DAA7C","326EB05C1D7E4BD4","6D44086C7354D1FA",  \
"64E883507170EC72","227B5C1E7815BB3E","17A3301FA17FC599","724B81715CFA3353","4EEB4616BDDDB9BE","45AAC2F465D2E689","5648E54CB2940D2A","64CC6148045B00F9","9412C697980F6F31","BC5DC951224B8DA4","5A080ECA5C179BE6","7FA010585221A737","78CF09C62BA35BA2","D1A3E98FD3A47E18","1C8220DFD65E5CEE","BF13A9DD6A442DD6","34240F2C954DEECB","65DC5AE4E4D56B06","9831F394905D111C","00748FF150C95DA7","E6E4E0EE417A710A","3C98E278FB5396DC","5113BE7B08FB9C3B","02A23993C1739EF7","CDF3EA71F9F13039","9B876FAD08E2D8DB","5A1BD56A6B8884F2","F3CECE66A44E6F0D","7151A9461325224F","FB4866D3B1C99DC6",  \
"34B5A4FCAF1183CA","C07E4C9F8235E2F5","D6169C677761A623","A1130E7EB235A215","2D6C3AB7112157EB","9878870A4C844D3D","1A7CBD58C3B7B6C4","F5C6C38D9863D173","42F509C7DF89E6B6","D5DB7C3E68DBC5E5","51E46B0AF653BC8E","0F4178D49CEEDBBC","7D2716C52911336E","26FC413230F5E98A","BF87600945186359","90C5564DEA58B96D","3553BF6B54A3A0EC","4A735B785A786491","CCA377A59788C909","448D4D9C3372BBB9","20AC2EF89D07AD53","0AD2523D195510AD","7FF5B71E8F7D340A","B32A520144A773A5","8DAFC597F551A5E5","7882D3C8AD88A36B","2EF21A986BE4AF4C","328091397F5B121E","3A8E1F3E726A9595","5F7C12BB7A678917",  \
"CAB9ACAB3059BBCC","E389CC46ECEF1A19","C828D59353B4595C","725339D5F7B43B69","E65E29697D9B86A5","26363B8D6D6A42C8","5E61F438DD0E037E","A8AB29C80EE1872A","77D8F5EAB8C1A1B5","70FC81C72020B9AF","000D6415309CB982","6A091AB0D56D1EA6","869920D0CDE39771","865AEF6B0EBB8B41","349AFE9C5657E951","20FA0E4EC9D0F729","C60978C777E4EA98","1425E6F434E440AC","3DA67DDF86431A21","08B50D0B4AE799DA","739D61C4EC763300","F8AAE16F350EFDE6","243786EA6A520C93","1AAD0464DA8480C3","21459133BEA63381","72F064605FD8C9D9","C9C3B67D0650B693","637D715D41232526","410FFCDBFE83C70F","8A12104D1559966E",  \
"78DE66841BE1C6A7","7ABD23957844A7C2","85E97200EC34D61A","5C8BA3F170D507A7","8730D08B81D0BCC1","9702A9FD1B0E5CA9","D768A4D58A023EEA","37E772A25297BEBB","FD507D619D0B69EA","A86B1FC888452328","7160EE3892F8833D","B37D9340D7FC8576","1770F25A1FC739E9","CC3BA6131D66EA90","649B36893E5B09E9","FA0B5DB0D130EB30","C17DD2C1B4BCA4DF","11C638B2C0D5A375","F06BEAD0F74DBB23","4556DF857884E0A3","1A70347725BFAD73","64EBED063582C2C7","E7B06229232B3B7B","C8F874C634E16F20","B9815AF1F9A54129","2A99FC364F6E5331","B49B0E4DCAFAE7EE","5E5CAA970C8A65A0","DAC0F075FE2588C3","BA2222D015279D21",  \
"A3BB2EED4BB17B35","CC114C6A64985A34","23E12D3DC330D7F5","AAB141AF001E48C3","1A339ADC2BEF79DB","8D98870111068F8A","41D7EB3C7C6BA1F2","C0B3762DD1CD4C5A","E455C477AB17835D","3EBA4D67B53C16E6","0DEE400649D2F73C","40018D194447A253","0416EBB5FF1FC72B","59C256A46F344F18","532EF53F039BD20D","2639BC900FBB9392","8D87B4566FA6E556","7A7C69CFD3C1AA61","E05B7F8B92A271A2","57A0073A31838990","CB16C7BB380AD3F2","718E2BFAB8E8014B","203B853955B0797A","86A66B852DEC8FA2","F1904608B6A75A6D","A09CD72FA4BC51F1","C6DF2C6EAE3FCCD2","9C81A6FEF25AC770","79FD70F5C056F331","70EA415F878C7419",  \
"736A1ADB201E07E5","C920479544FEB01A","A3153CEC8329898C","29B0009A3300AD0B","7E988DC41C1510B0","860ED5899EFA7174","FDD041034A79A0AE","77EEF7FC737ADE2E","D54A97906F3C0371","523AB29B4275E13E","24CC9791DCE23D98","6678A5E8340DF320","DAE9D3078909772E","C16FE8804ECB0FDB","FDD5B8834A086D6D","90469E83377DD56F","A43EF4DAB5D2F684","D5A90E77D50D612C","9304DA31C06D17BF","61227A9752AAEAF9","36139490277B0419","5B155F29F8588542","4A3CB66B48C289A3","57CEFBBBA7A6EFA8","8D311834B24C8335","EB79BE2E51EE2B37","CD6138692E7BC2FA","E19069D354D7AF70","D8F1544616B9D0C5","12DCF28D6382CB14",  \
"AA6B185B57F0B005","CD8E3B3F13ED5EBB","759FB8F25783B2C7","FA91B6FE2735FD2D","AB12192EB4128E1E","7B450E482303CA44","FC2A9A3AD7C03F47","C8EC4BD3CB1263CC","377CF818A0521941","B785912103BC8EEA","2B6D280AEE602711","00556C71356B426B","BCCA8E144691B5D0","7165574FACD3DF2B","14D6D53E74CADAA7","A21B09ABD7BBF7E4","E830B4C1D3E7F166","F5DB6AA97670BDA4","6A8555AC1C0B2E5A","6AA3F87153DFF934","52A4C938AC6024CD","CB35DAA8F456B686","C0197CB173309B8B","F3CECE66A44E6F0D","A19171319CA0A93A","5CE1F7E1BFDAC336","2A32D32120F719A9","94038B7817D8864A","988E67287451C0E5","A5D14B1DABA23B93",  \
"B504FF7DBB4510D5","E2D4E81CB2959D4B","D9FAA0BDC5BE9458","6F59A9C241429394","F83B896200D7D9FA","615C0B3F0F853813","0A64ED1AF204546E","89AEEF47D08C49F9","1139CD6938CA90C7","C3CEDBB9A8AB7A30","96CE38D661F435CF","0153BA859D8327EC","5D40809384A16517","66A57A0EF8014772","445944867494A731","7D575883C2D0F74F","DA17D96104BFA4FC","913A28DB0486EAB8","2CB518E5CBAC9172","5B4C64A6F25F150F","9056D9E6AB859E6E","F0162B70548FD9DE","04DDCCC38C8A3941","0199CD8408F7296D","AF0FCEAF1082CA77","7DEE28C73B8DDA3F","8015B6159DDB8F33","055799E47F99586F","2862E970DCC71E15","2341A6144C35BB28",  \
"DDE819EF11189664","367D472A4DC46B91","898681776471595D","E10731699C02A88D","E838D5ECAD03FD7C","4CE57DF084CB9E20","18F2C4351121916E","F6C64713A6E89510","A1714324676A76A4","66D0A751A81E9ED2","8384BE8F421D782E","4C867981696BB070","01DF1B33717B2718","B228C0D288BE34DE","F84DED6A6C98AE2E","8FE72628F6C94694","17A1C79A6AFC5B5A","D057DF1F8568A380","052211D64564E963","77E3460A46979DA5","BD55EB502C79CB8C","2369B4D35EF793FF","1D69EB30A059C9BD","9E5CE4F59BC23051","FF493B63872B4167","607A2DA989A704FE","A07B2CC605E94B95","BD8CCFF60DBCD966","76B1AABB3F431584","D509A9D5D3851FB8",  \
"E24902AB45E22A2F","5C8F9885CE2A3CA2","5A029B1AAA60D3C0","291F32CFDEB5AF79","49CC52520B7EEEDE","87D81EDFD1416740","57EEBE542596BFA4","49A528C41C9926F8","7C1F3A6AF182F913","D84E29A2FC8AF8E4","A803B38ED8E92E2E","F819AD0604220A7B","184BF9E1DF90F75F","B656C091BB452BB9","E8480969E3E312EA","6FD056566AFA5166","50D5E13401105145","02F9DC7D980AEFAF","9B61BCBE2715E1ED","4556DF857884E0A3","B52B345CB27B4A89","BFB3E45DC11B1FF4","BC9B498972150EA9","7E708725C73B2EDB","F4C0B2884949F444","A572C290C591A30A","CDF66F9A23BB2665","AD63624D808BAACB","76F6143457205690","59869C9DEEE21FEE",  \
"CBF506851D6D545A","5BC93108348995B3","C57F49C42AE6DFBB","4D404330F0955A11","A3974143A5D5C667","EEAFC9EC3BFB8038","12B1AA1EA1EFAA18","4B502A048B00BE0D","78D49284B67E9B49","2B69D0A30F756579","B855AAB118D4C557","04B1A85F61CD9909","3A8312B53E8886D0","D8C108DAD55F9A4D","7C06185B9EC4F6EB","44742BA46FDEF46F","3F220E0CD60B37C8","80410ADA7630A7EE","4AEBA13371D051E8","129C9BB53E406FF5","417C1A9C5B24AC48","A8616E1A6C3CC659","E37CFDB84518BC2C","CD57276057681A6E","83D4DD82B8E4AFB3","9DD427B773FCDD19","D4A78208359B2186","66062E9447E6259F","17628678769E5B64","338F45C0243341C9",  \
"2D8ECC7072E0D84D","1834BF8900D975F9","CAFAF22592F5FD74","80A7F2F7007E29FC","71A6089B594A7DFD","DC67B985245068C5","4C29C6373C81C220","48A3A662F1472717","FBE500C48BDB6B36","E1B19F18FF985F3C","4487DFF3D3A242C8","F6574B383109D3CA","FDAB9037124495FE","906D92DA2969927E","1AE14594A1F10037","BF5D1FF754EB28A6","B241F1CC38DE5C0C","BFBC81FDF07CA483","B94FF4272F0825E8","15501621F4D576CB","F0AA80FD539E9BC2","B44A4EE32A0BAAC2","A2294C612B8D3D76","8BE0D6B5FAE27D5A","94AEF5C6222C8BBA","B4496428972DA11E","2116DBB1314C5340","42672CDE8FB7D842","FD7F96010D6BAE0C","4D4C60DDEAE7756A",  \
"8C2E757FD5DDA25D","3777215E200FF96A","EFA4F6C94AA9631A","A75B2C1F7453AB7C","087E7B1C536FD99E","695BE2FB4CBB41AF","EB8FF7B3997518C0","EBB0081734B7B479","63E76FA8D97B0E45","BB4C7D8677DE3F5A","AF0F610FFAC2A9FC","89687F977710A655","0724DF6A80B97F54","173DFA6CD39A3010","C07A8E561E993978","2AA0F9E5BC255A66","D62615347B4077DC","751EAF23869ED9B6","3B3ABECBD4945E44","B7CFDC87A035FA71","96FCB844CA97D01F","9BAFF83AA34B95B9","48A838E68CB021BC","343298A9B5C4B750","6F5CF0BF288EB549","B8480527FC041991","93A8C6C75E6C6D6B","E04C43553017F396","0BC97DB7456EA1C3","5C568319E3F8FE65",  \
"D9D83FB926E17EAE","F7C515422FA1DE0F","44C6511E1C92F58A","6A98F132D69E1EB9","56230C8085CD1801","BBD82192DEE08D98","A7510D3246BF5B4A","1FEDB8FD0BA35EB6","2AC63FEB081C6603","3F7A001F6384EED7","5A880168DC7422D0","7B761483D21082B7","11440555BA3D2A09","BB8C037EADF3AEF3","05652952DA27F82A","E3757C16778CFBEF","1887C09B04F84FFB","4F2A5B08D0CC6FD0","CD1CAB3106227878","6A267CF8A7276BF9"]
#@NoUseNewKeyEx return 1 #如果没有使用这个功能，直接返回1
#@NoSupNewKeyEx return 2 #果该锁不支持这个功能，直接返回2
    myrnd=random.randint(1, (500 -1 ))
    mylen = len(EncInString[myrnd])+1
    if  mylen < 8 :
        mylen = 8 
    outstring = create_string_buffer((mylen* 2+1))#//注意，这里要??一个长度，用于储存结束学符串
    for n in range(0,255):
         ret=FindPort(n,DevicePath)
         if ret!=0:
             CloseDongle(DevicePath)
             return ret
         ret=EncString_New(EncInString[myrnd].encode('utf-8'), outstring,DevicePath)
         if outstring.value.decode('utf-8').lower()==EncOutString[myrnd].lower():
             CloseDongle(DevicePath)
             return 0
    CloseDongle(DevicePath)
    return -92


#使用增强算法一检查加密锁，这个方法可以有效地防止仿真
def CheckKeyByEncstring():
#推荐加密方案：生成随机数，让锁做加密运算，同时在程序中端使用代码做同样的加密运算，然后进行比较判断。
    DevicePath=create_string_buffer(260)
#@NoUseKeyEx return 1 #如果没有使用这个功能，直接返回1
    InString=('%X%X' % ((int)(random.uniform(0, 24174836)),(int)(random.uniform(0, 24174836))))
    for n in range(0,255):
         ret=FindPort(n,DevicePath)
         if ret!=0 :
            CloseDongle(DevicePath)
            return ret
         if Sub_CheckKeyByEncstring(InString,DevicePath)==0:
            CloseDongle(DevicePath)
            return 0
    return -92

def Sub_CheckKeyByEncstring(InString,DevicePath):
#//'使用增强算法对字符串进行加密
    nlen = len(InString) + 1
    if nlen < 8 :
          nlen = 8
    outstring = create_string_buffer((nlen * 2+1))#//注意，这里要加1一个长度，用于储存结束学符??
    outstring = StrEnc(InString, 'B63308ED7219D7FE0D5FCC284329C550'.encode('utf-8'))
    outstring_2 = create_string_buffer((nlen * 2+1))#//注意，这里要加1一个长度，用于储存结束学符??
    EncString(InString.encode('utf-8'),outstring_2,DevicePath)
    if outstring.lower()==outstring_2.value.decode('utf-8').lower():#//比较结果是否相符
         ret=0
    else:
         ret=-92
    return ret

#//使用带长度的方法从指定的地址读取字符串
def ReadStringEx(addr,DevicePath):
    InArray=c_ubyte*1
    blen = InArray(0)
#//先从地址0读到以前写入的字符串的长??
    ret = YReadEx(blen, addr, 1, 'FFFFFFFF'.encode('utf-8'), 'FFFFFFFF'.encode('utf-8'), DevicePath)
    if ret != 0 :
        return ''
    outstring=create_string_buffer(blen[0])		
#再从地址1读取指定长度的字符串
    ret = YReadString(outstring, addr+1, blen[0], 'FFFFFFFF'.encode('utf-8'), 'FFFFFFFF'.encode('utf-8'), DevicePath)
    if ret!=0:
        return ''
    return outstring.value


#//使用从储存器读取相应数据的方式检查是否存在指定的加密锁
def CheckKeyByReadEprom():
    DevicePath=create_string_buffer(260)
#@NoUseCode_data return 1 #如果没有使用这个功能，直接返回1
    for n in range(0,255):
        ret=FindPort(n,DevicePath)
        if ret!=0 :
            CloseDongle(DevicePath)
            return ret
        outstring=ReadStringEx(0,DevicePath)
        if(outstring=='2114'.encode('utf-8')):
            CloseDongle(DevicePath)
            return 0
    CloseDongle(DevicePath)
    return -92

#使用普通算法一查找指定的加密锁
def CheckKeyByFindort_2():
    DevicePath=create_string_buffer(260)
    ret=FindPort_2(0, 1, -1619045854, DevicePath)
    CloseDongle(DevicePath)
    return ret

def CloseDongle(DevicePath):
    if 'Linux' in platform.system():
        CloseUsbHandle(DevicePath)#关闭USB设备

